package org.apache.james.mime4j.util;

public final class ByteArrayBuffer
{
  private byte[] buffer;
  private int len;

  public ByteArrayBuffer(int paramInt)
  {
    if (paramInt < 0)
      throw new IllegalArgumentException("Buffer capacity may not be negative");
    this.buffer = new byte[paramInt];
  }

  private void expand(int paramInt)
  {
    byte[] arrayOfByte = new byte[Math.max(this.buffer.length << 1, paramInt)];
    System.arraycopy(this.buffer, 0, arrayOfByte, 0, this.len);
    this.buffer = arrayOfByte;
  }

  public void append(int paramInt)
  {
    int i = 1 + this.len;
    if (i > this.buffer.length)
      expand(i);
    this.buffer[this.len] = (byte)paramInt;
    this.len = i;
  }

  public void append(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
  {
    if (paramArrayOfByte == null);
    while (true)
    {
      do
      {
        return;
        if ((paramInt1 < 0) || (paramInt1 > paramArrayOfByte.length) || (paramInt2 < 0) || (paramInt1 + paramInt2 < 0) || (paramInt1 + paramInt2 > paramArrayOfByte.length))
          throw new IndexOutOfBoundsException();
      }
      while (paramInt2 == 0);
      int i = paramInt2 + this.len;
      if (i > this.buffer.length)
        expand(i);
      System.arraycopy(paramArrayOfByte, paramInt1, this.buffer, this.len, paramInt2);
      this.len = i;
    }
  }

  public void append(char[] paramArrayOfChar, int paramInt1, int paramInt2)
  {
    if (paramArrayOfChar == null);
    while (true)
    {
      do
      {
        return;
        if ((paramInt1 < 0) || (paramInt1 > paramArrayOfChar.length) || (paramInt2 < 0) || (paramInt1 + paramInt2 < 0) || (paramInt1 + paramInt2 > paramArrayOfChar.length))
          throw new IndexOutOfBoundsException();
      }
      while (paramInt2 == 0);
      int i = this.len;
      int j = i + paramInt2;
      if (j > this.buffer.length)
        expand(j);
      int k = paramInt1;
      for (int l = i; l < j; ++l)
      {
        this.buffer[l] = (byte)paramArrayOfChar[k];
        ++k;
      }
      this.len = j;
    }
  }

  public byte[] buffer()
  {
    return this.buffer;
  }

  public int byteAt(int paramInt)
  {
    return this.buffer[paramInt];
  }

  public int capacity()
  {
    return this.buffer.length;
  }

  public void clear()
  {
    this.len = 0;
  }

  public boolean isEmpty()
  {
    int i;
    if (this.len == 0)
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  public boolean isFull()
  {
    int i;
    if (this.len == this.buffer.length)
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  public int length()
  {
    return this.len;
  }

  public void setLength(int paramInt)
  {
    if ((paramInt < 0) || (paramInt > this.buffer.length))
      throw new IndexOutOfBoundsException();
    this.len = paramInt;
  }

  public byte[] toByteArray()
  {
    byte[] arrayOfByte = new byte[this.len];
    if (this.len > 0)
      System.arraycopy(this.buffer, 0, arrayOfByte, 0, this.len);
    return arrayOfByte;
  }

  public String toString()
  {
    return new String(toByteArray());
  }
}